package han.chensing.latos.maker.latos.file.transform.impl.melody;

import org.jaudiotagger.audio.SupportedFileFormat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import han.chensing.latos.common.level.LatosProject;
import han.chensing.latos.common.level.util.CacheHelper;
import han.chensing.latos.common.level.util.StreamUtil;
import han.chensing.latos.maker.latos.file.transform.ProjectTransformer;
import han.chensing.latos.maker.latos.file.transform.SupportSuffix;

@SupportSuffix({"mcz"})
public class MelodyTransformer extends ProjectTransformer {

    @Override
    public LatosProject genLatosProject(File file) {
        if (file==null)return null;
        try{
            CacheHelper cacheHelper = CacheHelper.getInstance();
            File mcFile=null;
            File musicFile=null;
            String musicFormat=null;
            ZipFile zipFile=new ZipFile(file,ZipFile.OPEN_READ);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()){
                ZipEntry zipEntry = entries.nextElement();
                String name = zipEntry.getName();
                String suffix = StreamUtil.getSuffix(name);
                if (suffix==null)continue;
                if (suffix.equalsIgnoreCase("mc")) {
                    mcFile = cacheHelper.createCache(suffix);
                    copyToFile(zipFile,zipEntry,mcFile);
                }
                if (matchMusicExtension(suffix)){
                    musicFile = cacheHelper.createCache(suffix);
                    musicFormat=suffix.toLowerCase(Locale.ROOT);
                    copyToFile(zipFile,zipEntry,musicFile);
                }
            }
            if (musicFile==null||mcFile==null)return null;
            MelodyParser melodyParser=new MelodyParser();
            LatosProject latosProject = melodyParser.parseMelodyChart(mcFile);
            latosProject.setMusicFormat(musicFormat);
            latosProject.setMusicBytes(StreamUtil.getFileStreamBytes(musicFile));
            latosProject.genMusic();
            zipFile.close();
            return latosProject;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private void copyToFile(ZipFile zipFile,ZipEntry zipEntry,File file) throws IOException {
        InputStream inputStream = zipFile.getInputStream(zipEntry);
        OutputStream outputStream= new FileOutputStream(file);
        StreamUtil.streamCopy(inputStream,outputStream);
        outputStream.close();
        inputStream.close();
    }

    private boolean matchMusicExtension(String ext){
        try {
            SupportedFileFormat.valueOf(ext.toUpperCase(Locale.ROOT));
            return true;
        }catch(IllegalArgumentException iae){
            return false;
        }
    }
}
